package com.cgx.tablet.beeper.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.cgx.tablet.beeper.R;
import com.cgx.tablet.beeper.api.ApiCallback;
import com.cgx.tablet.beeper.api.ApiUtil;
import com.cgx.tablet.beeper.base.BasePresenter;
import com.cgx.tablet.beeper.bean.WinTransBean;
import com.cgx.tablet.beeper.bean.response.ListResponse;
import com.cgx.tablet.beeper.bean.response.MsgResponse;
import com.cgx.tablet.beeper.utils.SharedPreferenceUtil;
import com.cgx.tablet.beeper.utils.ToastUtil;
import com.cgx.tablet.beeper.view.ILoginView;


/**
 * 登录操作
 */
public class LoginPresenter extends BasePresenter {

    private ILoginView view;

    public LoginPresenter(ILoginView view) {
        super(view);
        this.view = view;
    }

    public void windowList() {
        callApi(ApiUtil.apiService.windowList(), new ApiCallback<ListResponse<WinTransBean>>() {
            @Override
            public void onSuccess(final ListResponse<WinTransBean> response) {
                view.onGetWindowsSuccess(response.getData());
            }

            @Override
            public void onFail(String code, String msg) {
                view.onGetWindowsFail(code, msg);
            }
        });
    }

    public void login(final String account, final String password, WinTransBean winTransBean, final int pos) {
        if (winTransBean == null){
            if (TextUtils.isEmpty(SharedPreferenceUtil.getIP((Context) view))){
                view.onLoginFail("-1", ((Context)view).getString(R.string.ip_empty));
            }else {
                view.onLoginFail("-1", ((Context)view).getString(R.string.window_empty));
            }
            return;
        }
        if (TextUtils.isEmpty(account)) {
            view.onLoginFail("-1", ((Context)view).getString(R.string.login_hint_user));
            return;
        }
        if (TextUtils.isEmpty(password)) {
            view.onLoginFail("-1", ((Context)view).getString(R.string.login_hint_password));
            return;
        }
        callApi(ApiUtil.apiService.login(account, password, winTransBean.getWinId()), new ApiCallback<MsgResponse>() {
            @Override
            public void onSuccess(final MsgResponse response) {
                SharedPreferenceUtil.setAccount((Context) view, account);
                SharedPreferenceUtil.setPassword((Context) view, password);
                SharedPreferenceUtil.setWindow((Context) view, pos);
                view.onLoginSuccess();
            }

            @Override
            public void onFail(String code, String msg) {
                view.onLoginFail(code, msg);
            }
        });
    }

}
